/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package web.converters;

import ejb.CategoryBean;
import ejb.CategoryBeanLocal;
import entities.Category;
import javax.ejb.EJB;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

/**
 *
 * @author Alejandro
 */
public class CategoryConverter implements Converter {

    @EJB
    CategoryBeanLocal categoryBean;

    public CategoryConverter() {
        
    }

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        // String newName=(String) FacesContext.getCurrentInstance().getAttributes().get("newName");
        try{
        Category parent = null;
        if (value != null && !value.isEmpty()) {
            parent = new Category();
            parent.setIdCategory(Integer.valueOf(value));
        }
        return new Category("auxiliar", parent);
        }catch(NumberFormatException e){
            
            Integer idCat = Integer.valueOf(value.split(" ")[0]);
            Category ca= new Category("auxiliar");
            ca.setIdCategory(idCat);
            return ca;
        }
        //If u look below, I convert the object into a unique string, which is its id.
        //Therefore, I just need to write a method that query the object back from the 
        //database if given a id. getProjectById, is a method inside my Session Bean that
        //does what I just described
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        // return ((Category) value).getIdCategory().toString(); //--> convert to a unique string.
        return value.toString();
    }
}
